﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Sinacor.Infra.Core.Licensing.Data
{
    public class Licenca : ResourceAccess
    {
        public int SelecionarProximoIdLicenca()
        {
            int retorno = 0;

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                string sql = " SELECT NVL(MAX(COD_LICE),0)+1 FROM TFWDLICE ";
                

                command.CommandText = sql.ToString();
                retorno = Convert.ToInt32(this.ExecuteScalar(command));
            }

            return retorno;
        }

        public int SelecionarProximoIdLicencaHistorico()
        {
            int retorno = 0;

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                string sql = " SELECT NVL(MAX(NUM_SEQ_VERS_LICE),0)+1 FROM TFWDVERS_LICE ";
                

                command.CommandText = sql.ToString();
                retorno = Convert.ToInt32(this.ExecuteScalar(command));
            }

            return retorno;
        }

        public Sinacor.Infra.Core.Licensing.Licensing SelecionarEmpresaHistorico(Sinacor.Infra.Core.Licensing.LicensingCompanyModule library)
        {
            Sinacor.Infra.Core.Licensing.Licensing response = new Sinacor.Infra.Core.Licensing.Licensing();
            StringBuilder sql = new StringBuilder();
            string plus;
            int indiceCompany = 0;
            bool newCompany = false;
            bool newModule = false;
            Company company;
            Module module;

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" SELECT NUM_SEQ_VERS_LICE
                                        ,COD_CORR
                                        ,COD_MOD_LOGC
                                        ,DATA_EXP_LICE
                                        ,IND_MOD_ADIC  
                                  FROM HFWDVERS_LICE_MOD_CORR
                                  WHERE  NUM_SEQ_VERS_LICE  = :NUM_SEQ_VERS_LICE
                                  ORDER BY COD_CORR
                                          ,COD_MOD_LOGC
                                          ,DATA_EXP_LICE
                                          ,IND_MOD_ADIC");
                

                
                this.AddInParameter(command, "NUM_SEQ_VERS_LICE", DbType.Int32, library.DescriptionLicensingId);
                

                command.CommandText = sql.ToString();
                using (IDataReader reader = command.ExecuteReader())
                {
                    response.Companies = new List<Company>();
                    while (reader.Read())
                    {
                        response.Id = reader.GetValue<int>("NUM_SEQ_VERS_LICE");

                        
                        newCompany = true;

                        

                        response.Companies.ToList().ForEach(
                                                            matchCompany =>
                                                            {
                                                                if (matchCompany.CompanyId == reader.GetValue<int>("COD_CORR"))
                                                                    newCompany = false;
                                                            }
                              );
                        

                        
                        if (newCompany)
                        {
                            company = new Company { CompanyId = reader.GetValue<int>("COD_CORR") };
                            response.Companies.Add(company);
                            indiceCompany = response.Companies.Count - 1;
                        }
                        

                        

                        
                        //Quarda o newModule  e utiliza uma regra para adicionar uma novo Modulo
                        newModule = true;

                        
                        response.Companies[indiceCompany].Modules.ForEach(
                                                                matchModule =>
                                                                {
                                                                    if (matchModule.ID == reader.GetValue<int>("COD_MOD_LOGC"))
                                                                        newModule = false;
                                                                }
                                                        );
                        

                        
                        if (newModule)
                        {
                            module = new Module();

                            module.ID = reader.GetValue<int>("COD_MOD_LOGC");
                            module.GoodThru = reader.GetValue<DateTime>("DATA_EXP_LICE");
                            plus = reader.GetValue<string>("IND_MOD_ADIC");

                            switch (plus)
                            {
                                case "S":
                                    module.Plus = true;
                                    break;
                                case "N":
                                    module.Plus = false;
                                    break;
                                default:
                                    break;
                            }
                            response.Companies[indiceCompany].Modules.Add(module);
                        }
                        

                        

                    }
                }
            }
            return response;
        }

        public Sinacor.Infra.Core.Licensing.Licensing SelecionarLicencaAtual(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            Sinacor.Infra.Core.Licensing.Licensing response = new Sinacor.Infra.Core.Licensing.Licensing();
            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" SELECT LIC.COD_LICE,       
                                         LIC.DATA_CRIA_LICE,
                                         LIC.COD_ALIA,
                                         LIC.IND_SITU_LICE,              
                                         TLA.COD_TIPO_LICE,
                                         TLA.NOME_TIPO_LICE,       
                                         MAX(HTC.NUM_SEQ_VERS_LICE) AS NUM_SEQ_VERS_LICE              
                                  FROM TFWDLICE LIC
                                  INNER JOIN TFWDTIPO_LICE TLA       ON LIC.COD_TIPO_LICE = TLA.COD_TIPO_LICE
                                  INNER JOIN TFWDLICE_MOD_CORR LEM   ON LIC.COD_LICE      = LEM.COD_LICE
                                  INNER JOIN TFWDCORR EMP            ON LEM.COD_CORR      = EMP.COD_CORR
                                  INNER JOIN TFWDMOD_LOGC MOU             ON LEM.COD_MOD_LOGC       = MOU.COD_MOD_LOGC
                                  INNER JOIN HFWDLICE HTC  ON LIC.COD_LICE      = HTC.COD_LICE
                                  WHERE  LIC.COD_LICE  = :COD_LICE        
                                    
                                  GROUP BY LIC.COD_LICE,       
                                           LIC.DATA_CRIA_LICE,
                                           LIC.COD_ALIA,
                                           LIC.IND_SITU_LICE,              
                                           TLA.COD_TIPO_LICE,
                                           TLA.NOME_TIPO_LICE ");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                

                command.CommandText = sql.ToString();
                using (IDataReader reader = command.ExecuteReader())
                {
                    Active? active = null;
                    string ativo;

                    while (reader.Read())
                    {
                        response.Id = reader.GetValue<int>("COD_LICE");
                        response.Created = reader.GetValue<DateTime>("DATA_CRIA_LICE");
                        response.AliasId = reader.GetValue<int>("COD_ALIA");

                        ativo = reader.GetValue<string>("IND_SITU_LICE");
                        response.Treaty = (Sinacor.Infra.Core.Licensing.Treaty)reader.GetValue<int>("COD_TIPO_LICE");
                        response.Description.Id = reader.GetValue<int>("NUM_SEQ_VERS_LICE");

                        switch (ativo)
                        {
                            case "S":
                                active = Active.Ativo;
                                break;
                            case "N":
                                active = Active.Inativo;
                                break;
                            default:
                                active = null;
                                break;
                        }
                        response.Active = active;
                    }
                }
            }
            return response;
        }

        public List<Sinacor.Infra.Core.Licensing.Licensing> Selecionar(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            List<Sinacor.Infra.Core.Licensing.Licensing> response = new List<Sinacor.Infra.Core.Licensing.Licensing>();
            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {

				sql.AppendLine(@"SELECT   LIC.COD_LICE,       
                                           LIC.DATA_CRIA_LICE,
                                           LIC.COD_ALIA,
                                           LIC.IND_SITU_LICE,              
                                           TLA.COD_TIPO_LICE,
                                           TLA.NOME_TIPO_LICE,       
                                           EMP.COD_CORR,
                                           EMP.NOME_CORR,
                                           EMP.COD_CNPJ_CORR,
                                           EMP.COD_CORR_SINACOR,       
                                           MOU.COD_MOD_LOGC,
                                           MOU.NOME_MOD_LOGC,
                                           MOU.SIGL_MOD_LOGC,
                                           LEM.DATA_EXP_LICE,
                                           LEM.IND_MOD_ADIC,
                                           MAX(HTC.NUM_SEQ_HIST_LICE) AS NUM_SEQ_HIST_LICE, 
                                           ALI.NOME_ALIA
                                    FROM TFWDLICE LIC 
                                    LEFT JOIN TFWDTIPO_LICE TLA  ON LIC.COD_TIPO_LICE = TLA.COD_TIPO_LICE 
                                    LEFT JOIN TFWDLICE_MOD_CORR LEM ON LIC.COD_LICE        = LEM.COD_LICE 
                                    LEFT JOIN TFWDCORR EMP          ON LEM.COD_CORR        = EMP.COD_CORR 
                                    LEFT JOIN TFWDMOD_LOGC MOU           ON LEM.COD_MOD_LOGC         = MOU.COD_MOD_LOGC 
                                    LEFT JOIN HFWDLICE HTC          ON LIC.COD_LICE        = HTC.COD_LICE 
                                    LEFT JOIN TFWDALIA ALI          ON LIC.COD_ALIA        = ALI.COD_ALIA 
                                    WHERE  LIC.IND_SITU_LICE           = 'S'
                                    AND   (LIC.COD_LICE                = :COD_LICE           OR :COD_LICE           IS NULL)
                                    AND   (TO_DATE(LIC.DATA_CRIA_LICE) = :DATA_CRIA_LICE     OR :DATA_CRIA_LICE     IS NULL)                                    
                                    AND   (TLA.COD_TIPO_LICE           = :COD_TIPO_LICE      OR :COD_TIPO_LICE      IS NULL)
                                    AND   (LIC.COD_ALIA      = :COD_ALIA OR :COD_ALIA IS NULL) 
                                  
                                    GROUP BY LIC.COD_LICE, LIC.DATA_CRIA_LICE, LIC.COD_ALIA, LIC.IND_SITU_LICE, TLA.COD_TIPO_LICE, TLA.NOME_TIPO_LICE, EMP.COD_CORR, EMP.NOME_CORR, EMP.COD_CNPJ_CORR, EMP.COD_CORR_SINACOR, MOU.COD_MOD_LOGC, MOU.NOME_MOD_LOGC, MOU.SIGL_MOD_LOGC, LEM.DATA_EXP_LICE, LEM.IND_MOD_ADIC, ALI.NOME_ALIA");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                this.AddInParameter(command, "DATA_CRIA_LICE", DbType.DateTime, library.Created);
                this.AddInParameter(command, "COD_TIPO_LICE", DbType.Int32, (int?)library.Treaty);
                this.AddInParameter(command, "COD_ALIA", DbType.Int32, library.AliasId);
                
                sql.AppendLine(@" ORDER BY LIC.COD_LICE,
                                           LIC.COD_ALIA, 
                                           EMP.COD_CORR,
                                           MOU.COD_MOD_LOGC ");
                

                command.CommandText = sql.ToString();
                using (IDataReader reader = command.ExecuteReader())
                {
                    Licensing licensing = null;
                    Licensing licensingAux = null;
                    Company company = null;
                    Module module = null;

                    Active? active = null;
                    string ativo;
                    int indiceCompany = 0;
                    bool newLicensing = false;
                    bool newCompany = false;
                    bool newModule = false;

                    string plus = string.Empty;

                    while (reader.Read())
                    {
                        
                        licensingAux = response.Find(match => match.Id == reader.GetValue<int>("COD_LICE"));

                        if (licensingAux == null)
                        {
                            licensing = new Licensing();

                            licensing.Id = reader.GetValue<int>("COD_LICE");
                            licensing.Created = reader.GetValue<DateTime>("DATA_CRIA_LICE");
                            licensing.AliasId = reader.GetValue<int>("COD_ALIA");

                            ativo = reader.GetValue<string>("IND_SITU_LICE");
                            licensing.Treaty = (Sinacor.Infra.Core.Licensing.Treaty)reader.GetValue<int>("COD_TIPO_LICE");

                            licensing.Description.Id = reader.GetValue<int>("NUM_SEQ_HIST_LICE");
							licensing.AliasName = reader.GetValue<string>("NOME_ALIA");

                            switch (ativo)
                            {
                                case "S":
                                    active = Active.Ativo;
                                    break;
                                case "N":
                                    active = Active.Inativo;
                                    break;
                                default:
                                    active = null;
                                    break;
                            }
                            licensing.Active = active;
                            //Nova Licença
                            newLicensing = true;
                        }
                        else
                            //Mesma Licença
                            newLicensing = false;
                        

                        
                        //Atribui valor ao newCompany  e utiliza uma regra para adicionar uma nova Empresa
                        newCompany = true;

                        

                        if (response.Count > 0)
                        {
                            if (response[response.Count - 1].Id == licensing.Id)
                            {                                
                                foreach (var item in response[response.Count - 1].Companies)
                                {
                                    if (item.CompanyId == reader.GetValue<int>("COD_CORR"))
                                     {
                                         newCompany = false;
                                         break; 
                                     }
                                }
                                //response.ToList().ForEach(
                                //              match => match.Companies.ForEach(
                                //                                            matchCompany =>
                                //                                            {
                                //                                                if (matchCompany.CompanyId == reader.GetValue<int>("COD_CORR"))
                                //                                                    newCompany = false;
                                //                                            }

                                //              )
                                //      );
                            }
                        }
                        

                        
                        if (newCompany)
                        {
                            company = new Company
                            {
                                CompanyId = reader.GetValue<int>("COD_CORR"),
                                Name = reader.GetValue<string>("NOME_CORR"),
                                LegalRegistrationId = reader.GetValue<string>("COD_CNPJ_CORR"),

                                SinacorCompanyId = reader.GetValue<int>("COD_CORR_SINACOR"),
                            };
                            licensing.Companies.Add(company);
                            indiceCompany = licensing.Companies.Count - 1;
                        }
                        
                        

                        
                        //Quarda o newModule  e utiliza uma regra para adicionar uma novo Modulo
                        newModule = true;

                        

                        if (response.Count > 0)
                        {
                            foreach (var item in response[response.Count - 1].Companies[indiceCompany].Modules)
                            {
                                if (item.ID == reader.GetValue<int>("COD_MOD_LOGC"))
                                {
                                    newModule = false;
                                    break;
                                }
                            }
                        }
                        //response.ToList().ForEach(
                        //              match => match.Modules.ForEach(
                        //                                            matchModule =>
                        //                                            {
                        //                                                if (matchModule.ID == reader.GetValue<int>("COD_MOD_LOGC"))
                        //                                                    newModule = false;
                        //                                            }

                        //              )
                        //      );
                        

                        
                        if (newModule)
                        {
                            module = new Module();

                            module.ID = reader.GetValue<int>("COD_MOD_LOGC");
                            module.Name = reader.GetValue<string>("NOME_MOD_LOGC");
                            module.Monogram = reader.GetValue<string>("SIGL_MOD_LOGC");

                            module.GoodThru = reader.GetValue<DateTime>("DATA_EXP_LICE");
                            plus = reader.GetValue<string>("IND_MOD_ADIC");

                            switch (plus)
                            {
                                case "S":
                                    module.Plus = true;
                                    break;
                                case "N":
                                    module.Plus = false;
                                    break;
                                default:
                                    break;
                            }
                            //Adiciona os Modulos na Empresa
                            if (licensing.Companies[indiceCompany].Modules == null)
                                licensing.Companies[indiceCompany].Modules = new List<Module>();

                            licensing.Companies[indiceCompany].Modules.Add(module);
                        }
                        
                        

                        if (newLicensing)
                        {
                            response.Add(licensing);
                        }
                    }
                }
            }
            return response;
        }

        public List<Sinacor.Infra.Core.Licensing.Licensing> SelecionarHistorico(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            List<Sinacor.Infra.Core.Licensing.Licensing> response = new List<Sinacor.Infra.Core.Licensing.Licensing>();

            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" SELECT HTC.NUM_SEQ_HIST_LICE
                                        ,THC.COD_TIPO_ATLZ_HIST
                                        ,THC.NOME_TIPO_ATLZ_HIST
                                        ,LCC.COD_LICE
                                        ,LCC.DATA_CRIA_LICE
                                        ,LCC.COD_ALIA
                                        ,LCC.IND_SITU_LICE
                                        ,TLC.COD_TIPO_LICE
                                        ,TLC.NOME_TIPO_LICE
                                        ,HTC.COD_USUA
                                        ,HTC.DATA_HIST
                                        ,HTC.TXT_COME
										,ALI.NOME_ALIA 
                                  FROM  HFWDLICE HTC
                                  INNER JOIN TFWDTIPO_ATLZ_HIST THC 
                                    ON HTC.COD_TIPO_ATLZ_HIST = THC.COD_TIPO_ATLZ_HIST
                                  INNER JOIN TFWDLICE LCC 
                                    ON HTC.COD_LICE = LCC.COD_LICE
                                  INNER JOIN TFWDTIPO_LICE TLC
                                    ON LCC.COD_TIPO_LICE = TLC.COD_TIPO_LICE
								  INNER JOIN TFWDALIA ALI 
									ON LCC.COD_ALIA = ALI.COD_ALIA 
                                  WHERE (HTC.NUM_SEQ_HIST_LICE  = :NUM_SEQ_HIST_LICE  OR :NUM_SEQ_HIST_LICE IS NULL) 
                                  AND   (HTC.COD_TIPO_ATLZ_HIST = :COD_TIPO_ATLZ_HIST OR :COD_TIPO_ATLZ_HIST IS NULL) 
                                  AND   (HTC.COD_LICE           = :COD_LICE           OR :COD_LICE IS NULL) 
                                  AND   (LCC.COD_ALIA = :COD_ALIA OR :COD_ALIA IS NULL)                                     
                                  AND   (HTC.COD_USUA           = :COD_USUA           OR :COD_USUA IS NULL) 
                                  AND   (TO_DATE(HTC.DATA_HIST) = :DATA_HIST          OR :DATA_HIST IS NULL) 
                                  ORDER BY LCC.COD_LICE DESC
                                          ,HTC.DATA_HIST DESC  
                                          ,LCC.COD_ALIA ");

                

                
                this.AddInParameter(command, "NUM_SEQ_HIST_LICE", DbType.Int32, library.Description.Id);
                this.AddInParameter(command, "COD_TIPO_ATLZ_HIST", DbType.Int32, library.Description.TypeDescription);
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                this.AddInParameter(command, "COD_ALIA", DbType.Int32, library.AliasId);
                this.AddInParameter(command, "COD_USUA", DbType.String, library.Description.User);
                this.AddInParameter(command, "DATA_HIST", DbType.DateTime, library.Description.Period);

                

                command.CommandText = sql.ToString();

                using (IDataReader reader = command.ExecuteReader())
                {
                    Licensing licensing = null;
                    Active? active = null;
                    string ativo;
                    string plus = string.Empty;

                    while (reader.Read())
                    {
                        licensing = new Licensing();
                        licensing.Id = reader.GetValue<int>("COD_LICE");
                        licensing.Created = reader.GetValue<DateTime>("DATA_CRIA_LICE");
                        licensing.AliasId = reader.GetValue<int>("COD_ALIA");

                        licensing.Treaty = (Sinacor.Infra.Core.Licensing.Treaty)reader.GetValue<int>("COD_TIPO_LICE");
                        ativo = reader.GetValue<string>("IND_SITU_LICE");

						licensing.AliasName = reader.GetValue<string>("NOME_ALIA");

                        switch (ativo)
                        {
                            case "S":
                                active = Active.Ativo;
                                break;
                            case "N":
                                active = Active.Inativo;
                                break;
                            default:
                                active = null;
                                break;
                        }
                        licensing.Active = active;
                        licensing.Description = new Description();
                        licensing.Description.Id = reader.GetValue<int>("NUM_SEQ_HIST_LICE");
                        licensing.Description.TypeDescription = (TypeDescription)reader.GetValue<int>("COD_TIPO_ATLZ_HIST");
                        licensing.Description.User = reader.GetValue<string>("COD_USUA");
                        licensing.Description.Period = reader.GetValue<DateTime>("DATA_HIST");
                        licensing.Description.Annotation = reader.GetValue<string>("TXT_COME");
                        response.Add(licensing);
                    }
                }
            }

            return response;
        }

        private List<Sinacor.Infra.Core.Licensing.Company> GetEmpresa(Sinacor.Infra.Core.Licensing.Company _empresa)
        {
            List<Sinacor.Infra.Core.Licensing.Company> retorno = new List<Sinacor.Infra.Core.Licensing.Company>();

            retorno.Add(_empresa);

            return retorno;
        }

        private List<Sinacor.Infra.Core.Licensing.Module> GetModulo(Sinacor.Infra.Core.Licensing.Module _modulo)
        {
            List<Sinacor.Infra.Core.Licensing.Module> retorno = new List<Sinacor.Infra.Core.Licensing.Module>();

            retorno.Add(_modulo);

            return retorno;
        }

        public void InserirLicenca(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                

                sql.AppendLine(@" INSERT INTO TFWDLICE
                                            (COD_LICE
                                            ,COD_TIPO_LICE
                                            ,DATA_CRIA_LICE
                                            ,COD_ALIA
                                            ,IND_SITU_LICE)
                                      VALUES
                                            (:COD_LICE
                                            ,:COD_TIPO_LICE
                                            ,:DATA_CRIA_LICE
                                            ,:COD_ALIA
                                            ,:IND_SITU_LICE)");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                this.AddInParameter(command, "COD_TIPO_LICE", DbType.Int32, library.Treaty);
                this.AddInParameter(command, "DATA_CRIA_LICE", DbType.DateTime, library.Created);
                this.AddInParameter(command, "COD_ALIA", DbType.Int32, library.AliasId);

                string activo = "";
                switch (library.Active)
                {
                    case Sinacor.Infra.Core.Licensing.Active.Ativo:
                        activo = "S";
                        break;
                    case Sinacor.Infra.Core.Licensing.Active.Inativo:
                        activo = "N";
                        break;
                    default:
                        break;
                }

                this.AddInParameter(command, "IND_SITU_LICE", DbType.String, activo);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void InserirLicencaEmpresaHistorico(Sinacor.Infra.Core.Licensing.LicensingCompanyModule library)
        {
            StringBuilder sql = new StringBuilder();
            string plus;
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                

                sql.AppendLine(@" INSERT INTO HFWDVERS_LICE_MOD_CORR
                                        (NUM_SEQ_VERS_LICE
                                        ,COD_CORR
                                        ,COD_MOD_LOGC
                                        ,DATA_EXP_LICE
                                        ,IND_MOD_ADIC)
                                  VALUES
                                        (:NUM_SEQ_VERS_LICE
                                        ,:COD_CORR 
                                        ,:COD_MOD_LOGC
                                        ,:DATA_EXP_LICE
                                        ,:IND_MOD_ADIC) ");
                

                
                this.AddInParameter(command, "NUM_SEQ_VERS_LICE", DbType.Int32, library.DescriptionLicensingId);
                this.AddInParameter(command, "COD_CORR", DbType.Int32, library.CompanyId);
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ModuleId);
                this.AddInParameter(command, "DATA_EXP_LICE", DbType.DateTime, library.GoodThru);

                plus = library.Plus ? "S" : "N";
                this.AddInParameter(command, "IND_MOD_ADIC", DbType.String, plus);

                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void InserirLicencaHistorico(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" INSERT INTO TFWDVERS_LICE
                                        (NUM_SEQ_VERS_LICE                                        
                                        ,COD_TIPO_LICE
                                        ,DATA_CRIA_LICE
                                        ,COD_ALIA)
                                  VALUES
                                        (:NUM_SEQ_VERS_LICE
                                        ,:COD_TIPO_LICE
                                        ,:DATA_CRIA_LICE
                                        ,:COD_ALIA)");
                

                
                this.AddInParameter(command, "NUM_SEQ_VERS_LICE", DbType.Int32, library.Description.IdVersionLicensing);
                this.AddInParameter(command, "COD_TIPO_LICE", DbType.Int32, library.Description.TypeDescription);

                this.AddInParameter(command, "DATA_CRIA_LICE", DbType.DateTime, library.Created);
                this.AddInParameter(command, "COD_ALIA", DbType.Int32, library.AliasId);

                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void InserirLicencaEmpresaModulo(Sinacor.Infra.Core.Licensing.LicensingCompanyModule library)
        {
            StringBuilder sql = new StringBuilder();
            string plus;
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" INSERT INTO TFWDLICE_MOD_CORR
                                        (COD_LICE
                                        ,COD_CORR
                                        ,COD_MOD_LOGC
                                        ,DATA_EXP_LICE
                                        ,IND_MOD_ADIC)
                                   VALUES
                                        (:COD_LICE
                                        ,:COD_CORR
                                        ,:COD_MOD_LOGC
                                        ,:DATA_EXP_LICE
                                        ,:IND_MOD_ADIC)");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.LicensingId);
                this.AddInParameter(command, "COD_CORR", DbType.Int32, library.CompanyId);
                this.AddInParameter(command, "COD_MOD_LOGC", DbType.Int32, library.ModuleId);
                this.AddInParameter(command, "DATA_EXP_LICE", DbType.DateTime, library.GoodThru);

                plus = library.Plus ? "S" : "N";
                this.AddInParameter(command, "IND_MOD_ADIC", DbType.String, plus);

                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void ExcluirLicencaHistorico(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" DELETE FROM TFWDLICE_MOD_CORR 
                                  WHERE  COD_LICE = :COD_LICE ");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void RestaurarEmpresaModulo(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" DELETE FROM TFWDLICE_MOD_CORR 
                                  WHERE  COD_LICE = :COD_LICE ");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void AlterarLicenca(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                
                sql.AppendLine(@" UPDATE TFWDLICE SET 
                                         COD_TIPO_LICE       = :COD_TIPO_LICE                                        
                                        ,COD_ALIA  = :COD_ALIA
                                        ,IND_SITU_LICE       = :IND_SITU_LICE
                                   WHERE COD_LICE = :COD_LICE");




                this.AddInParameter(command, "COD_TIPO_LICE", DbType.Int32, library.Treaty);

                this.AddInParameter(command, "COD_ALIA", DbType.Int32, library.AliasId);

                string activo = "";
                switch (library.Active)
                {
                    case Sinacor.Infra.Core.Licensing.Active.Ativo:
                        activo = "S";
                        break;
                    case Sinacor.Infra.Core.Licensing.Active.Inativo:
                        activo = "N";
                        break;
                    default:
                        break;
                }

                this.AddInParameter(command, "IND_SITU_LICE", DbType.String, activo);


                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        public void ExcluirLicenca(Sinacor.Infra.Core.Licensing.Licensing library)
        {
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                StringBuilder sql = new StringBuilder();
                
                sql.Append(@" UPDATE TFWDLICE
                              SET IND_SITU_LICE = 'N'
                              WHERE COD_LICE = :COD_LICE");
                

                
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.Id);
                

                command.CommandText = sql.ToString();
                this.ExecuteNonQuery(command);
            }
        }
    }
}